<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta name="author" content="zhaozhenguo">
		<title>礼品验收入库单</title>
		<!--js库及样式-->
		<script LANGUAGE="javascript" src="../script/common.js" type="text/javascript"></script>
		<!--动态js-->
		<script language="javascript" type="text/javascript">
			//全局变量
			var org = Common.cOrg();	//当前单位
			var oper=Common.cOperator();
			var qt = {};				//数据表格对象
			var qtmx = {};
			var qtmxchange={};
			var caigouid;				//申请单号 
			var orderid;				//GiftOrder
		    var giftid;
	         var br="true";
	         var flagtype;
	         var isval;
	         var calculateTypelist=[{code:"1",text:"验收入库单"},{code:"2",text:"验收入库更正单"}];
			//页面加载方法
			function load(){
				Eui.showNavigation();
				setPages(["listview","itemlists","insertview","updateview","newinventoryorder","itemlistschange","printview"]);	
				createTable();
			}
			function createTable(){
				var columns = [];
				columns[0]={type:"checkbox"}
				columns[7]={title:"操作",width:"10%",render:showItems,order:false}
				columns[8]={title:"操作",width:"10%",render:changeOrder,order:false};
				
				/*var columns = [	
					{type:"checkbox"},
					{title:"礼品验收入库单号",width:"25%"},
					{title:"参考外部单据号",width:"20%"},
					{title:"礼品管理员",width:"20%"},
					{title:"验收入库时间",width:"20%",render:function(v){return v?v.format():""}},
					{title:"操作",width:"15%",render:showItems,order:false}
				]*/
				qt = new QueryTable("loy_gift_instore_query_bill","listform","datalist",columns,{
			
					buttons:[
						{value:"增 加",actions:onNewOrder},
						{value:"删 除",actions:onDeleteOrder}
					]

				});
							
			}
			function changeOrder(v,r){
			if(r[3]==2){
               	return "<a href=# onclick=\><font color=\"gray\"></font></a>"
               }
			if(r[4]){
			  return "<a href=# onclick=\><font color=\"gray\">创建更正单</font></a>"
		
			}else if(r[7]){
			  return "<a href=# onclick=\"changeOrder2("+r[0]+");return false;\">创建更正单</a>"				
			}
			else{
			return "<a href=# onclick=\><font color=\"gray\">创建更正单</font></a>"
			}
               
			}
			function itemCreateTable(){
				var columns =[];
				columns[0]={type:"checkbox"}
				columns[7]={title:"操作",width:"15%",render:updateItem,order:false}
				/*var columns = [
					{type:"checkbox"},		//复选框
					{title:"商品类型",width:"15%"},	
					{title:"商品子类型",width:"15%"},	                      //点击进入修改
					{title:"商品名称",width:"15%"},						
					{title:"入库数量",width:"10%"},					
					{title:"计量单位",width:"10%"},
					{title:"备注",width:"20%"},
					{title:"操作",width:"15%",render:updateItem,order:false}
				]*/
				qtmx = new QueryTable("loy_gift_instore_query_item","listform1","itemlist",columns,{showData:"true",
					buttons:[
						{value:"增 加",id:"add",actions:onNewItem},
						{value:"删 除",id:"delete",actions:onDeleteItems},
						{value:"返 回",actions:resetFunc}
					]
				});
				qtmx.paramFunc = function(){
					var params = {};
					params.giftOrder = orderid;
					return params;
				}
			}
			function itemCreateTableChange(){
				var columns =[];
				columns[0]={type:"checkbox"}
				columns[7]={title:"操作",width:"15%",render:updateItem,order:false}
				/*var columns = [
					{type:"checkbox"},		//复选框
					{title:"商品类型",width:"15%"},	
					{title:"商品子类型",width:"15%"},	                      //点击进入修改
					{title:"商品名称",width:"15%"},						
					{title:"入库数量",width:"10%"},					
					{title:"计量单位",width:"10%"},
					{title:"备注",width:"20%"},
					{title:"操作",width:"15%",render:updateItem,order:false}
				]*/
				qtmxchange = new QueryTable("loy_gift_instore_query_change","listform2","itemlist2",columns,{showData:"true",
					buttons:[
						{value:"增 加",actions:onNewItem},
						{value:"删 除",actions:onDeleteItems},
						{value:"返 回",actions:resetFunc}
					]
				});
				qtmxchange.paramFunc = function(){
					var params = {};
					params.giftOrder2 = orderid;
					return params;
				}
			}
			function toHOS(){
			  	DWR.call('giftStorageService.toHos',orderid,function(v){  
			  	giftId =v.id;
			  	requestframe.document.location.href = "print_instore_gift.html";
			  	switchPage("printview"); 
			});
			}
			function back(){
				createTable();
				showData();
				switchPage('listview');			
			}
			function showData(){
				qt.showTable();
			}
			function showDataChange(){
				qtmxchange.showTable();
			}
			function showItems(v,r){
				return "<a href=# onclick=\"showItems2("+r[0]+",'"+r[1]+"','"+r[2]+"','"+r[3]+"','"+r[4]+"','"+r[5]+"','"+r[6]+"');return false;\">操作</a>"
			}
			function updateItem(v,r){
			     if(br=="false"){
			      return "<a href=# onclick=\><font color=\"gray\">修改</font></a>"
			     }else{
			     return "<a href=# onclick=\"updateItem2("+r[0]+",'"+r[3]+"','"+r[4]+"','"+r[5]+"','"+r[6]+"');return false;\">修改</a>"
			     }
				
			}
		    function updateItem2(id,giftName,reqQty,baseUOM,comments){
		            
			    if (id != null){
			    	 $("updateform").id.value=id;
			        $("giftName2").innerHTML=giftName;
			        $("updateform").quantity.value=reqQty;
			        $("baseUOM2").innerHTML=baseUOM;
			        if(comments=="null"){
			        	comments="";
			      	}
			        $("updateform").comments.value=comments;
				  	switchPage("updateview");
				}
			}
			var codeids;
			var codeids2;
			var dates;
			function showItems2(id,codeid,codeid2,type,isvalue,oper,date){
				br=isvalue;
				orderid=id;
			 	if (id != null){
			            $("orderCode").innerHTML=codeid;
			 		  $("refCode").innerHTML=codeid2;
	                      $("operator").innerHTML=oper;
	                      $("instoretype").innerHTML=type;
	                      $("isvalues").innerHTML=isvalue;
	                    
	                   $("checkDateTime").innerHTML=date;
			       	itemCreateTable();
					switchPage("itemlists");		 		
				}
			
				 if(isvalue=="否"){
				 $("toHOSform").bin.disabled=true;
				 $("itemlist_btn_delete").disabled = true;
				  $("itemlist_btn_add").disabled = true;
			 	    }
			}
			function changeOrder2(id){
			br="true"
			if(showQuestion("您确认创建更正单?")==false){
			  return;
			  }	
			DWR.call('giftStorageService.iOrderToChange',id,function(v){    
			          $("orderCode2").innerHTML=v.orderCode;
			 		$("refCode2").innerHTML=v.refCode;
	                    $("operator2").innerHTML=v.operator;
	                    $("instoretype2").innerHTML=v.flag;
	                    $("isvalues2").innerHTML=v.isValidate;
	                    var date=new Date(v.checkDateTime).format();
	                    $("checkDateTime2").innerHTML=date;
	                    $("listform2").giftOrder2.value=v.id;
	                    orderid=v.id;
			       	itemCreateTableChange();
			          showDataChange();
					switchPage("itemlistschange");
			   }); 		
			}
			//显示新增界面
			function update2(){
				if(!checkForm(updateform))return;
			    var id=updateform.id.value;
			    var qu=updateform.quantity.value;
			    var comments=updateform.comments.value;
				DWR.call('giftStorageService.updateInventoryItem',id,qu,comments,function(v){
			    	itemCreateTable(); 
			        if(v){
			        	showError("您输入的数量差值大于库存数量，对数量的修改失败");
			     	}
			    });
				switchPage('itemlists');
				
			}
			function onNewOrder(){
				switchPage("newinventoryorder");
			}
			//选择单位控件
			function selOrg(){			
				var v = openDialogFrame("/client/giftrule_list.html",'选择商品',500,600)
				if (v){		
	                insertform.giftName.value = v.name;
				    giftid=v.id;
				}
			}
			function selOrg2(){			
				var v = openDialogFrame("/client/giftrule_list.html",'选择商品',500,600)
				if (v){		
	            	$("caigoumxxz").giftName.value = v.name;
				    giftid=v.id;
				}
			}
			//取消方法
			function resetFunc(){
				switchPage('listview');
				showData();
			}
			//保存方法
			function onSaveOrAddOrder(){
				if(!checkForm("inventoryorder"))return;
					var inventoryorder1 = Common.getValues("inventoryorder");
					if (!inventoryorder1.id)
					inventoryorder1.operator=oper.id;
					inventoryorder1.flag=1;
					inventoryorder1.giftLocation=org.id;         
					DWR.call('giftStorageService.createInventoryOrder',inventoryorder1,Constants.GIFT_INVENTORY_IN,function(v){
					    	 $("toHOSform").bin.disabled=false;
						switchPage('insertview');
						getOrderId(v.id,v.orderCode,v.flag,v.isValidate,v.refCode,v.checkDateTime);    
					}); 
			}  
			function getOrderId(id,orderCode,flags,idval,refCode,checkDateTime){
					var datetime=new Date(checkDateTime);
					orderid=id;
					flagtype=flags;
					isval=idval;
					codeids=orderCode;
					codeids2=refCode;
					dates=datetime.format();      
			}	
			function onSaveOrAddItem(){
				if(!checkForm(insertform))return;
		        var inventoryitem = Common.getValues(insertform);
			    if (!inventoryitem.id)
		    	inventoryitem.inventoryOrder=orderid;
		        inventoryitem.gift=giftid;
		      
			    DWR.call('giftStorageService.addInventoryItem',inventoryitem,function(v){
				    if(v){
		            	showError("添加失败，已有此商品了");
		            }else{
		                showInformation("添加成功!");
		            }     
	            });
	         	insertform.reset();
			}
			function onNewItem(){
					switchPage('insertview'); 
			}
			function qx(){
				switchPage('selectdetview');
			}
			function xzFunc(){
				$("caigoumxxz").reset();
				switchPage('detviewmxxz');
			}
			function resetItem(){
		    	insertform.reset();
			}
			function resetItem2(){
		    	insertform.reset();
			}
			//删除方法
			function onDeleteItems(){
				var	id = qtmx.getBoxValue();
				if(id==null||id==""){
					showWarning("请选择您确定要删除的数据");return;
				}
				if(showQuestion("确定删除吗?")==false){
					return;
				}  	
				var ids = id.split(",");
				DWR.call('giftStorageService.deleteInventoryItemByIds',ids,function(v){
		        	itemCreateTable(); 
		            showInformation("删除成功!");
			    });
				switchPage('itemlists');
			}
			function onDeleteOrder(){  
				var	id = qt.getBoxValue();
				if(id==null||id==""){
			    	showWarning("请选择您确定要删除的数据");return;
				}
				if(showQuestion("确定删除吗?")==false){
					return;
				}  	
				var ids = id.split(",");		
				DWR.call('giftStorageService.deleteInventoryOrderByIds',ids,function(v){
		           	showData();
		           	if(v==1){
		            	     showError("有些单据，由于库存数量不足不能删除");
		          	}else if(v==2){
		          		showError("有些单据，由于不可编辑状态不能删除");
		          	}else{
		          	showInformation("删除成功!");
		          	}
		            	
		           
			    });
				switchPage('listview');
			}
	        function onBackItemList(){ 
	              $("toHOSform").bin.disabled=false;
	           	itemCreateTable();
			    if(codeids==null){
			    }else{
				     $("orderCode").innerHTML=codeids;
				     $("instoretype").innerHTML=flagtype;
	                    $("isvalues").innerHTML=isval;
				 	$("refCode").innerHTML=codeids2;
		               $("operator").innerHTML=oper.name;
		               $("checkDateTime").innerHTML=dates;   
			    }
	            switchPage('itemlists');
	          }
			function onCancl1(){
				switchPage('listview');
			}
			function onCancl2(){
				switchPage('itemlists');
			}
		</script>
	</HEAD>
	<body onload="load()">
		<!--页面导航-->
		<div class="nav"><div id="nav">&nbsp;</div></div>
		<!--查询静态页面部分-->
		<div id="listview">	
			<div class="searchForm">
				<form id="listform" name="listform">
					<table cellSpacing=0 cellPadding=4 width="100%" border=0 >
					    <tr>
						    <td><label>验收入库单号</label> <input type=text name="instoCode" maxlength=15></td>
						    <td><label>验收入库日期</label> <input type=text name="instoDate" onblur="checkV(this,'date')"><a href="javascript:void(0)" onclick='showDatePicker( $("instoDate"));' class="date" title="选择日期">&nbsp;&nbsp;</a></td>
						    <td><button type="button" onclick="showData()">查 询</button></td>
						</tr>
					</table>
				</form>
			</div>
			<div id="datalist"></div>
		</div>
		
		<div id="itemlists" style="display:none;">
			<div class="form">
				<form id="listform1" name="listform1">
					<input type="hidden" name="giftOrder">
					<input type="hidden" name="organization">	
					<div class="formTitle">礼品验收入库单详细信息</div>
					<table cellSpacing=0 cellPadding=4 width="100%" border=0 class="viewTable">
						<tr>
							<td><label>验收入库单号</label> <span id="orderCode"></span></td>
							<td><label>参考外部单据号</label> <span id="refCode"></span></td>
						</tr>
						<tr>
							<td><label>入&nbsp;库&nbsp;类&nbsp;型&nbsp;</label> <span id="instoretype" dict="inventory_flag"></span></td>
							<td><label>是&nbsp;&nbsp;否&nbsp;&nbsp;提&nbsp;&nbsp;交</label> <span id="isvalues" dict="inventory_iseditable"></span></td>
						</tr>
						<tr>		
							<td><label>验收入库日期</label> <span id="checkDateTime"> </span></td>
						    <td><label>操&nbsp;&nbsp;&nbsp;&nbsp;作&nbsp;&nbsp;&nbsp;&nbsp;员</label> <span id="operator"></span></td>
						</tr>
					</table>
				</div>	 
			</form>
			<div id="itemlist"></div>
			<form id="toHOSform" name="toHOSform">
			   <button type="button" id=bin onclick="toHOS()">TOHOS</button>
			</form>
		</div>
		<div id="selectdetview" style="display:none;" align="center"></div>
		<div class="printview" id="printview" style='display:none;'>
			<iframe id="requestframe" name="requestframe" style="width:100%;height:90%;overflow:auto;" frameborder=0 src=""></iframe>
		</div>		
		<div id="itemlistschange" style="display:none;">
			<div class="form">
				<form id="listform2" name="listform2">
					<input type="hidden" name="giftOrder2">
					<input type="hidden" name="organization2">	
					<div class="formTitle">礼品验收入库更正单详细信息</div>
					<table cellSpacing=0 cellPadding=4 width="100%" border=0 class="viewTable">
						<tr>
							<td><label>验收入库更正单号</label> <span id="orderCode2"></span></td>
							<td><label>参考外部单据号</label> <span id="refCode2"></span></td>
						</tr>
						<tr>
							<td><label>类型</label> <span id="instoretype2"></span></td>
							<td><label>是&nbsp;否&nbsp;提&nbsp;交&nbsp;</label> <span id="isvalues2"></span></td>
						</tr>
						<tr>		
							<td><label>验收入库更改日期</label> <span id="checkDateTime2"> </span></td>
						    <td><label>操&nbsp;&nbsp;&nbsp;&nbsp;作&nbsp;&nbsp;&nbsp;&nbsp;员</label> <span id="operator2"></span></td>
						</tr>
					</table>
				</div>	 
			</form>
			<div id="itemlist2"></div>
		</div>
		<!--明细(新增)静态页面部分-->
		<div id="insertview" style="display:none;">
			<form id="insertform" name="insertform">
				<input type="hidden" name="id">
				<input type="hidden" name="organization">
				<div class=form>
				<div class="formTitle">商品详细信息</div>
					<table cellSpacing=0 cellPadding=4 width="100%" border=0>
						<tr>
							<td><label>商品名称</label> <input type="text" name="giftName" onblur="checkV(this,'',true)" readonly class="readonly"><a class=orga href=# onclick="selOrg()" return false;><font class="des">*</font></td> 
							<td><label>入库数量</label> <input type=text name="quantity" onblur="checkV(this,'number',true)" maxlength=8><font class="des">*</font></td>
						</tr>
						<tr>		
							<td><label>备&nbsp;&nbsp;&nbsp;&nbsp;注</label> <input type="text" name="comments" maxlength="32"></td>
						</tr>		
					</table>
				</div>
				<div align="center"><button type="button" onclick="onSaveOrAddItem()">添 加</button> <button type="button" onclick="resetItem()">清 除</button> <button type="button" onclick="onBackItemList()">返 回</button></div>
			</form>
		</div>
		
		<div id="updateview" style="display:none;">
			<form id="updateform" name="updateform">
				<input type="hidden" name="id">
				<input type="hidden" name="organization">
				<div class="form">
				<div class="formTitle">修改商品信息</div>
					<table cellSpacing=0 cellPadding=4 width="100%" border=0 class="viewTable">
						<tr>
							<td><label>商品名称</label> <span id="giftName2"></span></td>
							<td><label>入库数量</label> <input type="text" name="quantity" onblur="checkV(this,'number',true)" maxlength=8><font class="des">*</font></td>
						</tr>
						<tr>		
							<td><label>计量单位</label> <span id="baseUOM2"/></td>
							<td><label>备&nbsp;&nbsp;&nbsp;&nbsp;注</label> <input type="text" name="comments"  maxlength="32"></td>
						</tr>			
					</table>
				</div>
				<div align="center"><button type="button" onclick="update2()">保 存</button> <button type="button" onclick="onCancl2()">取 消</button></div>
			</form>
		</div>
		
		<div id="newinventoryorder" style="display:none;">
			<form id="inventoryorder" name="inventoryorder">
				<div class="form">
				<div class="formTitle">添加入库单据</div>
					<table cellSpacing=0 cellPadding=4 width="100%" border=0>	
						<tr>
							<td><label>参考外部单据号</label> <input type="text" name="refCode" onblur="checkV(this,'',true)" maxlength=15><font class="des">*</font></td> </td>
						</tr>	
					</table>
				</div>
				<div align="center"><button type="button" onclick="onSaveOrAddOrder()">保 存</button> <button type="button" onclick="onCancl1()">取 消</button></div>
			</form>
		</div>
	</body>
</HTML>